﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework;

namespace QS.Game.Renderer
{
    public class QGlassRenderer
    {
        private DynamicVertexBuffer m_VertexBuffer;
        private DynamicIndexBuffer m_IndexBuffer;
        private Effect m_Effect;

        private GraphicsDevice m_GraphicsDevice;
        private ContentManager m_ContentManager;
        private Texture2D m_Texture;
        BasicEffect Basic3DEffect;
        public QGlassRenderer(GraphicsDevice graphicsDevice, ContentManager contentManager)
        {
            m_GraphicsDevice = graphicsDevice;
            m_ContentManager = contentManager;
            m_VertexBuffer = new DynamicVertexBuffer(m_GraphicsDevice, typeof(VertexPositionNormalTexture), 100, BufferUsage.WriteOnly);
            m_IndexBuffer = new DynamicIndexBuffer(m_GraphicsDevice, typeof(short), 300, BufferUsage.WriteOnly);
            m_Effect = m_ContentManager.Load<Effect>("Effect1");
            m_Texture = m_ContentManager.Load<Texture2D>("glass");

            Basic3DEffect = new BasicEffect(graphicsDevice);
            Basic3DEffect.Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, graphicsDevice.Viewport.AspectRatio, 0.1f, 1000.0f);
            Basic3DEffect.TextureEnabled = true;
            Basic3DEffect.Texture = contentManager.Load<Texture2D>("glass");
            Basic3DEffect.EnableDefaultLighting();
        }

        public void Draw(VertexPositionNormalTexture[] _vertices, short[] _indices, QCamera camera)
        {

            m_VertexBuffer.SetData<VertexPositionNormalTexture>(_vertices);
            m_IndexBuffer.SetData<short>(_indices);

            m_GraphicsDevice.SetVertexBuffer(m_VertexBuffer);
            m_GraphicsDevice.Indices = m_IndexBuffer;
            m_Effect.Parameters["World"].SetValue(Matrix.Identity);
            m_Effect.Parameters["View"].SetValue(camera.ViewMatrix);
            m_Effect.Parameters["Projection"].SetValue(camera.ProjectionMatrix);
            m_Effect.Parameters["xTexture"].SetValue(m_Texture);
            Basic3DEffect.View = camera.ViewMatrix;
            Basic3DEffect.Projection = camera.ProjectionMatrix;
            RasterizerState rasterizerState = new RasterizerState();
            rasterizerState.CullMode = CullMode.None;
            m_GraphicsDevice.RasterizerState = rasterizerState;
            m_GraphicsDevice.DepthStencilState = DepthStencilState.Default;
            foreach (EffectPass pass in Basic3DEffect.CurrentTechnique.Passes)
            {
                pass.Apply();
                m_GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, _vertices.Length, 0, _indices.Length / 3);
            }
            m_GraphicsDevice.SetVertexBuffer(null);
            m_GraphicsDevice.Indices = (null);
        }

    }
}
